#ifndef         CONSUMERPARSER_H
# define        CONSUMERPARSER_H

#include <map>
#include <string>
#include "LoaderXML.h"

# define        SAVE_CONTEXT() \
  unsigned int  tmp = this->index; \

# define        RESTORE_CONTEXT() \
  this->index = tmp; \

# define        NEW_BLOC() \
  if (!this->_bloc[this->index]) \
    this->getBloc(); \

class	ConsumerParser
{
public:
  ConsumerParser(LoaderXML &stream);
public:
  void                  getBloc();
  inline bool           peekChar(char c) { return (this->_bloc[this->index] == c); }

  inline bool                   readChar(char c)
  {
    if (this->peekChar(c))
      {
        this->index++;
        NEW_BLOC();
        return true;
      }
    return false;
  }

  inline bool   readRange(char begin, char end)
  {
    if (this->_bloc[index] >= begin && this->_bloc[index] <= end)
      {
        this->readChar(this->_bloc[this->index]);
        return true;
      }
    return false;
  }

  inline bool   beginCapture(std::string tag) { this->cap[tag] = this->index; return true; }

  inline bool   endCapture(std::string tag, std::string& out)
  {
    if (this->cap[tag])
      {
        out = this->_bloc.substr(this->cap[tag], this->index);
        return true;
      }
    return false;
  }

  bool          readText(char *text);
  bool          readEOF();
  bool          readUntil(char c);
  bool          readUntilEOF();
  bool          readInteger();
  bool          readIdentifier();
  bool          readExpr(ConsumerParser& parse);

  std::string	getTagContent();

private:
  LoaderXML				&_stream;
  std::string				_bloc;
  unsigned int				index;
  std::map<std::string, unsigned int>	cap;
};

#endif  //      !CONSUMERPARSER_HPP
